<?
/*
 * Galeri, version 0.1
 * (c) 2007 Fajran Iman Rusadi.
 *
 * Galeri is freely distributable under the terms of an MIT-style license.
 *
 */


include('config.php');
include('common.php');

// Config

$maxWidth = $_image_max_width;
$maxHeight = $_image_max_height;

// Image
$img = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';

$pathinfo = getValidPath($img);
$img = $pathinfo['path'];

// Cache

$cacheFile = $_cache_dir . md5($img);
if (!file_exists($cacheFile)) {

	$path = $_images_dir . '/' . $img;
	
	$info = getimagesize($path);
	$width = $info[0];
	$height = $info[1];
	$type = $info['mime'];
	
	$tWidth = $width;
	$tHeight = $height;
	
	if (($tWidth > $maxWidth) || ($tHeight > $maxHeight)) {
		$ratio = $width / $height;
		$mRatio = $maxWidth / $maxHeight;
		if ($ratio > $mRatio) {
			$tHeight = intval(($tHeight / $tWidth) * $maxWidth);
			$tWidth = $maxWidth;
		}
		else {
			$tWidth = intval(($tWidth / $tHeight) * $maxHeight);
			$tHeight = $maxHeight;
		}
	}
	
	$thumb = imagecreatetruecolor($tWidth, $tHeight);
	$orig = imagecreatefromstring(file_get_contents($path));
	imagecopyresampled($thumb, $orig, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);

	imagejpeg($thumb, $cacheFile, 80);
}

header('Content-type: image/jpeg');
print(file_get_contents($cacheFile));


?>
